package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.OrdersDto;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 提交订单
     * @param order
     * @return
     */
    @PostMapping("/order/submit")
    public R submitOrder(@RequestBody Orders order){

        int row = orderService.add(order);

        if(row > 0 ){

            shoppingCartService.clean();

            return R.success("提交订单成功！");
        }
        return R.success("提交订单失败！");

    }

    /**
     * 查询最新订单和历史订单
     * @param pageParam
     * @return
     */
    @GetMapping("/order/userPage")
    public R latestOrder(PageParam pageParam){
        IPage<OrdersDto> page = orderService.page(pageParam);
        return R.success(page);
    }

    @GetMapping("/order/page")
    public R page(PageParam pageParam){
        IPage<OrdersDto> page = orderService.findPage(pageParam);

        System.out.println("page================== = " + page.getRecords());

        //2. 响应结果
        return R.success(page);
    }

    @PutMapping("/order")
    public R update(@RequestBody Orders orders){
        int row = orderService.update(orders);

        if(row > 0){
            return R.success("更新菜品成功！");
        }

        return R.error("更新菜品失败！");
    }
}
